package zhr.chapter9;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class MouseEventDemo extends JFrame {
    private JTextField textField;
    private JLabel label;
    private int count;
    public MouseEventDemo() {
        textField = new JTextField(10);
        this.add(textField);
        label = new JLabel();
        this.add(label);
        this.setLayout(new FlowLayout());
        this.addMouseListener(new MouseAdapter() {
            public void mouseExited(MouseEvent e){
                label.setText("鼠标移出窗体");
            }
            public void mouseEntered(MouseEvent e){
                label.setText("鼠标移入窗体");
            }
            public void mouseReleased(MouseEvent e){
                count++;
                textField.setText("鼠标被单击"+count+"次!");
            }
            public void mousePressed(MouseEvent e){
                textField.setText("鼠标键按下!");
            }
        });
        this.addMouseMotionListener(new MouseMotionAdapter() {
           public void mouseMoved(MouseEvent e){
               label.setText("鼠标在窗体上移动的坐标: "+e.getX()+","+e.getY());
           }
           public void mouseDragged(MouseEvent e){
               label.setText("鼠标在窗体上拖动的坐标: "+e.getX()+","+e.getY());
           }
        });
        textField.setFocusable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100,100,600,300);
        this.setVisible(true);
        this.setTitle("鼠标事件演示");
    }

}
